package com.kdtech.analyse.app;
import java.util.HashMap;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.analyse.AnalyseNews;

/**
 * @链接 http://kb.qq.com/
 * @名字 天天快报
 * @作者 dhy
 */
public class QQAppAnalyse implements AnalyseNews {

	private static final String[] regex={
		"http://openapi.kuaibao.qq.com/getSubNewsContent\\?id=[A-Z0-9]+&isShowTitle=1"
	};


	public static HashMap<String, String> getLinks(String entryUrl){
		if(StringUtils.isBlank(entryUrl)) return null;
		HashMap<String, String> links = new HashMap<String, String>();
//		UrlMeta response = CrawlHTML.responseToURL(entryUrl);
		UrlMeta response = null;
		String htmltxt = response.getHtml();
		htmltxt = StringUtils.substringAfter(htmltxt, "responseData(");
		htmltxt = StringUtils.substringBeforeLast(htmltxt, ")");
		JSONObject object = JSONObject.parseObject(htmltxt);
		JSONArray array = object.getJSONArray("idlist");
		if(array == null || array.size() == 0)
			return null;
		object = array.getJSONObject(0);
		JSONArray ids = object.getJSONArray("ids");
		for(int i = 0; i < ids.size(); i++){
			String id = ids.getJSONObject(i).getString("id");
			if(StringUtils.isNotBlank(id)){
				System.out.println("http://kb.qq.com/article.htm?id="+id+"&commentid=0");
				links.put("http://openapi.kuaibao.qq.com/getSubNewsContent?id=" + id + "&isShowTitle=1", "");
			}
		}

		System.out.println(links);
		return links;
	}

	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		if(StringUtils.isBlank(htmltxt))
			return null;
		String url=urlMeta.getUrl();

//		String id = URLUtil.getValue(url, "id");
		String id = "";
		url = "http://kb.qq.com/article.htm?id="+id+"&commentid=0";

		news.setUrl(url);
		String title=null;
		String content=null;
		String author=null;
		Long date=null;

		String data = StringUtils.substringBetween(htmltxt, "content", "},");
		if(StringUtils.isBlank(data)) return null;

		title = StringUtils.substringBetween(data, "\"titleName\":\"", "\",\"");
		if(StringUtils.isBlank(title)) return null;
		title = StringUtils.decodeUnicode(title, true);

		date = DateUtils.matchDate(url);

		content = StringUtils.substringBetween(data, "\"text\":\"", "\",\"");
		System.out.println(content);
		if(StringUtils.isNotBlank(content)){
			content = StringUtils.decodeUnicode(content, true);
			if(content!=null){
				content=content.replace("\\", "");
			}
		}

//		System.out.println(content);
		news.setTitle(StringUtils.trimSpace(title));
		news.setAuthor(author);
		news.setContent(content);
		news.setDate(date);

		return news;

	}

	

	

	
}

